package com.example.shaidemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;

import java.util.ArrayList;
import java.util.List;

public class ChaHangyeDetailActivity extends AppCompatActivity {
    private LineChart line;
    private ListView listView;

    List<Entry> list=new ArrayList<>();
    private List<Gongzi> salary_list=new ArrayList<>();
    GongziAdapter gongziAdapter;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cha_hangye_detail);
        listView=findViewById(R.id.home_gongzi_list);
        toolBar();
        salary_list.add(0,new Gongzi(1,"保险顾问","销售","金融销售","6833","56","3","深圳","中国平安证券"));
        salary_list.add(1,new Gongzi(2,"JAVA","技术","后端开发","15780","436","5","成都","中软国际"));
        salary_list.add(2,new Gongzi(3,"Web前端","技术","前端开发","17900","1154","2","杭州","杭州阿里巴巴网络科技有限公司"));
        salary_list.add(3,new Gongzi(4,"产品经理","产品","产品经理","31600","339","9","北京","百度"));
        salary_list.add(4,new Gongzi(5,"JAVA","技术","后端开发","47300","2158","10","上海","蚂蚁金服网络技术有限公司"));


        gongziAdapter = new GongziAdapter(this, R.layout.list_item_gongzi_shouye, salary_list);
        listView.setAdapter(gongziAdapter);
        setListViewHeightBasedOnChildren(listView);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //跳转到详情页
                Intent intent = new Intent(ChaHangyeDetailActivity.this, GongziDetailActivity.class);
                startActivity(intent);
            }
        });

//        line = (LineChart) findViewById(R.id.line);
//
//        list.add(new Entry(0,97560));     //其中两个数字对应的分别是   X轴   Y轴
//        list.add(new Entry(1,158000));
//        list.add(new Entry(2,206400));
//        list.add(new Entry(3,244590));
//        list.add(new Entry(4,316200));
//
//        //list是你这条线的数据  "语文" 是你对这条线的描述（也就是图例上的文字）
//        LineDataSet lineDataSet=new LineDataSet(list,"年薪收入分位值");
//        LineData lineData=new LineData(lineDataSet);
//        line.setData(lineData);
//
//
//        //简单美化
//
//        line.getDescription().setEnabled(false);                  //是否显示右下角描述
//
//        //   X轴所在位置   默认为上面
//        line.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
//        //X轴
//        XAxis xAxis=line.getXAxis();
//        xAxis.setLabelCount(5, true);
//        xAxis.setDrawGridLines(false);  //是否绘制X轴上的网格线（背景里面的竖线）
//            xAxis.setAxisLineColor(Color.BLACK);   //X轴颜色
//        //xAxis.setAxisLineWidth(2);           //X轴粗细
//        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);        //X轴所在位置   默认为上面
//        xAxis.setValueFormatter(new IAxisValueFormatter() {   //X轴自定义坐标
//            @Override
//            public String getFormattedValue(float v, AxisBase axisBase) {
//                if (v==0.5){
//                    return "10分位";
//                }
//                if (v==1.5){
//                    return "25分位";
//                }
//                if (v==3){
//                    return "50分位";
//                }if (v==4){
//                    return "75分位";
//                }if (v==5){
//                    return "90分位";
//                }
//                return "";//注意这里需要改成 ""
//            }
//        });
//        xAxis.setAxisMaximum(5);   //X轴最大数值
//        xAxis.setAxisMinimum(0);   //X轴最小数值
//        //X轴坐标的个数    第二个参数一般填false     true表示强制设置标签数 可能会导致X轴坐标显示不全等问题
//        xAxis.setLabelCount(5,false);
//
//        //隐藏右边的Y轴
//        line.getAxisRight().setEnabled(false);

    }
    public void toolBar(){
        boolean isImmersive = false;
        final TitleBar titleBar = (TitleBar) findViewById(R.id.title_bar);

        titleBar.setLeftImageResource(R.drawable.ic_left_white_24dp);
        //titleBar.setTitle();
        titleBar.setLeftText("技术");
        titleBar.setTitleColor(R.color.white);

        titleBar.setLeftTextSize(18);
        titleBar.setLeftTextColor(getResources().getColor(R.color.white));
        titleBar.setLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                finish();
            }
        });

        titleBar.setTitleColor(Color.BLACK);

        //沉浸式
        titleBar.setImmersive(isImmersive);
    }

    public void setListViewHeightBasedOnChildren(ListView listView) {
        // 获取ListView对应的Adapter
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }

        int totalHeight = 0;
        for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
            // listAdapter.getCount()返回数据项的数目
            View listItem = listAdapter.getView(i, null, listView);
            // 计算子项View 的宽高
            listItem.measure(0, 0);
            // 统计所有子项的总高度
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        // listView.getDividerHeight()获取子项间分隔符占用的高度
        // params.height最后得到整个ListView完整显示需要的高度
        listView.setLayoutParams(params);
    }
}